package com.open.capacity.workflow.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import com.open.capacity.workflow.dao.WFConfluenceDao;
import com.open.capacity.workflow.model.WFConfluence;
import com.open.capacity.workflow.service.WFConfluenceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class WFConfluenceServiceImpl implements WFConfluenceService {

    @Autowired
    private WFConfluenceDao confluenceDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(WFConfluence confluence) {
        confluence.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        confluenceDao.save(confluence);
        log.info("保存会签：{}", confluence);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(WFConfluence confluence) {
        confluenceDao.updateByOps(confluence);
        log.info("修改会签：{}", confluence);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        WFConfluence confluence = confluenceDao.findById(id);
        confluenceDao.delete(id);

        log.info("删除会签：{}", confluence);
    }

    @Override
    public void delete(String processId, String nodeId) {
        confluenceDao.deleteByProcessAndNode(processId, nodeId);
        log.info("删除会签：processId{},nodeId{}", processId, nodeId);
    }


    @Override
    public WFConfluence findById(String id) {
        return confluenceDao.findById(id);
    }

    @Override
    public WFConfluence findTasksByProcessIdAndPreviousId(String processId, String fromNodeId) {
        return confluenceDao.findTasksByProcessIdAndPreviousId(processId, fromNodeId);
    }

    @Override
    public List<WFConfluence> findConfluences(Map<String, Object> params) {
//        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<WFConfluence> list = confluenceDao.findList(params);
        return list;
    }


    @Override
    public Result saveOrUpdate(WFConfluence confluence, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            WFConfluence model = confluenceDao.findById(keyValue);
            if (model != null) {
                confluence.setId(keyValue);
                i = confluenceDao.updateByOps(confluence);
            }
        } else {
            confluence.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            i = confluenceDao.save(confluence);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
